# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=s2geometry
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.11.1
pkgrel=6
pkgdesc="A library for manipulating geometric shapes (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://s2geometry.io/'
msys2_repository_url='https://github.com/google/s2geometry'
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-s2geometry-0.10.0-fix-python-install.patch"
        "0002-s2geometry-0.10.0-use-math-defines.patch")
sha256sums=('bdbeb8ebdb88fa934257caf81bb44b55711617a3ab4fdec2c3cfd6cc31b61734'
            '41da8d28e6eaad9d5d706f4110ef1ec3a95aa20715e962a5780045875e54d6a3'
            'c3b518b783a8df07bbd7e3fadd9b79606ffdc5fec607d3f9320a03657ab2ebf0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    0001-s2geometry-0.10.0-fix-python-install.patch \
    0002-s2geometry-0.10.0-use-math-defines.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTS=OFF \
      -DWITH_PYTHON=ON \
      -DPython3_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package_s2geometry() {
  depends=("${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-abseil-cpp")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-${_realname}=${pkgver}-${pkgrel}: Python bindings")

  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-s2geometry() {
  pkgdesc="Python bindings for s2geometry (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python-pywraps2")
  provides=("${MINGW_PACKAGE_PREFIX}-python-pywraps2")
  replaces=("${MINGW_PACKAGE_PREFIX}-python-pywraps2")

  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . --component python

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :
# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
